<?php

namespace app\agent\controller;

use app\agent\model\Withdraw as WithdrawModel;
use app\agent\validate\Withdraw as WithdrawValidate;

class Withdraw extends Controller
{
    protected function initialize()
    {
        parent::initialize();
    }

    /**
     * 列表.
     */
    public function list()
    {
        $model = new WithdrawModel();
        $data = input('post.');
        $data['agent_id'] = $this->agent['id'];

        return $model->getList($data);
    }

    /**
     * @api {POST} /agent/withdraw/create 提现申请
     * @apiDescription 提现申请
     * @apiName agentWithdrawCreate 提现申请
     * @apiParam {Number} amount 提现金额
     * @apiParam {String} alipay 支付宝
     * @apiParam {String} mobile 手机号
     * @apiParam {String} wechat 微信号
     * @apiGroup Admin2_0_agent
     * @apiUse BaseReturn
     * @apiVersion 1.0.0
     */
    public function create()
    {
        $data = input('post.');
        $validate = new WithdrawValidate();
        if (empty($data['type']) || !in_array($data['type'], [1, 2])) {
            return fail('提现方式有误，请刷新重试');
        }
        $scene = 'alipay';
        if ($data['type'] == 2) {
            $scene = 'bank';
        }

        if (!$validate->scene($scene)->check($data)) {
            return fail($validate->getError());
        }

        $model = new WithdrawModel();

        return $model->create($this->agent['id'], $data);
    }

    /**
     * 撤销
     */
    public function cancel()
    {
        $data = input('post.');
        $model = new WithdrawModel();
        $data['agent_id'] = $this->agent['id'];

        return $model->cancel($data);
    }

    /**
     * 提现配置
     */
    public function getConfig()
    {
        $info = \think\facade\Db::name('agent_withdraw_config')
            ->field('memo')
            ->where('agent_id', 0)
            ->find();
        $info['memo'] = nl2br($info['memo']);
        return success('操作成功', $info);
    }
}
